<?php
/**
 * 类工厂
 * User: t.young
 * Date: 2017/12/17
 * Time: 11:09
 */

namespace common\component;


use common\exceptions\ClassNotFoundException;
use yii\base\Exception;
use yii\base\NotSupportedException;
use yii\web\HttpException;

class ClassFactory
{
    private static $_instance = [];

    private function __construct()
    {
    }

    public static function getInstance($className) {
        if (self::$_instance && in_array($className,self::$_instance)) {
            return self::$_instance[$className];
        }
        try{
            self::$_instance[$className] = new $className;
            return self::$_instance[$className];
        }catch (Exception $e) {
            \Yii::error("类工厂实例化错误：【{$className}】,原因：".$e->getMessage());
            return false;
        }

    }

    public function __clone()
    {
        throw new NotSupportedException("禁止复制");
    }
}